package com.aaa.train.controller;


import com.aaa.common.entity.LayUiTable;
import com.aaa.common.entity.MyConstants;
import com.aaa.common.entity.ResultData;
import com.aaa.common.entity.TbCourse;
import com.aaa.train.service.TbCourseService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 学科表 前端控制器。
 * </p>
 *
 * @author wang
 * @since 2020-07-29
 */
@Controller
@RequestMapping("/tbCourse")
public class TbCourseController {

    @Autowired
    private TbCourseService tbCourseService;


    @ResponseBody
    @RequestMapping("/selectAll")
    public LayUiTable selectAll(Integer page, Integer limit,String tbCourseName){
        Wrapper<TbCourse> wrapper = new EntityWrapper();
        if (tbCourseName!=null&&!tbCourseName.equals("")){
            wrapper.like("tb_course_name",tbCourseName);
        }
        wrapper.eq("status",0);
        Page<TbCourse> pageSize = new Page(page,limit);
        Page<TbCourse> pages = tbCourseService.selectPage(pageSize,wrapper);
        //返回多少条
//        int size = deptService.selectList(null).size();
        Integer i = tbCourseService.selectCount(wrapper);
        LayUiTable layUiTable = new LayUiTable();
        layUiTable.setCode(MyConstants.OPERATION_SUCCESS_CODE);
        layUiTable.setMsg(MyConstants.OPERATION_SUCCESS_MESSAGE);
        layUiTable.setData(pages.getRecords());
        layUiTable.setCount(i);
        return layUiTable;
    }
    @ResponseBody
    @RequestMapping("/insert")
    public ResultData insert(TbCourse tbCourse){
        boolean id = tbCourseService.insert(tbCourse);
        if (id){
            return  new ResultData(true,"新增成功",null);
        }else{
            return  new ResultData(false,"新增失败",null);

        }
    }
    @ResponseBody
    @RequestMapping("/update")
    public ResultData update(TbCourse tbCourse){
        if (tbCourse.getStatus() == null){
            tbCourse.setStatus(1);
        }
        boolean id = tbCourseService.updateById(tbCourse);
        if (id){
            return  new ResultData(true,"修改成功",null);
        }else{
            return  new ResultData(false,"修改失败",null);

        }
    }
    /*
     * description: 理论删除
      * @param null
     * @return
     */
    
    @ResponseBody
    @RequestMapping("/delete")
    public ResultData delete(TbCourse tbCourse){
        tbCourse.setStatus(1);
        boolean id = tbCourseService.updateById(tbCourse);
        if (id){
            return  new ResultData(true,"删除成功",null);
        }else{
            return  new ResultData(false,"删除失败",null);

        }
    }

}

